<!--- *** FUNCTIONS *** --->
<!--- 1. Function to process label --->
<cffunction name="getlabel" returntype="void" output="true">
	<cfargument name="config" type="struct"/>
	<cfargument name="excludes" required="false" default="label"/>
	<cfset var it = 0/>
<label<cfloop collection="#config#" item="it"><cfif right(it,5) eq "label" and not listFindNoCase(excludes,it)> #lcase(left(it,len(it)-5))#="#config[it]#"</cfif></cfloop>>#config.label#<cfif config.required><span class="#config.classRequired#">*</span></cfif>:</label>
</cffunction>

<!--- 2. Function to process field attributes --->
<cffunction name="getFieldAttributes" output="true" returntype="void">
	<cfargument name="att" type="struct"/>
	<cfargument name="excludes"/><cfloop collection="#att#" item="it"><cfif not listFindNoCase(excludes,it) and right(it,5) neq "label"> #lcase(it)#="#att[it]#"</cfif></cfloop></cffunction>

<!--- 3. Function to return config struct in children tags --->
<cffunction name="getConfig" returntype="struct">
	<cfargument name="field">
	<cfargument name="att" type="struct"/>
	<cfargument name="global"/>
	<cfargument name="parent"/>
	<cfset var config = duplicate(att)/>
	<!--- Getting cf_form original config variables --->
	<cfif structKeyExists(arguments,'parent')>
		<cfset structAppend(config,parent.formtag.config,false)/>	
	</cfif>
	<!--- Getting config variables of a specific tag name from request scope --->
	<cfif isDefined("global.ui.form.#field#.name.#att.name#.config")>
		<cfset structAppend(config, global.ui.form[field].name[att.name].config, false)/>	
	</cfif>
	<!--- Getting config variables of a tag from request scope --->
	<cfif isDefined("global.ui.form.#field#.config")>
		<cfset structAppend(config, global.ui.form[field].config, false)/>
	</cfif>
		<!--- Adding all remaining cf_form config variables --->
	<cfif structKeyExists(arguments,'parent')>
		<cfset structAppend(config, parent.config, false)/>
		<!--- For parent tag: Getting config variables of all tags from request scope --->
	<cfelseif isDefined("global.ui.form.all.config")>
		<cfset structAppend(config, global.ui.form.all.config, false)/>
	</cfif>
	<cfreturn config/>
</cffunction>

<!--- 4. Function to return html attribute struct in children tags --->		
<cffunction name="getHtml" returntype="struct">
	<cfargument name="field"/>
	<cfargument name="att" type="struct"/>
	<cfargument name="global" type="struct"/>
	<!--- Getting config variables of a specific tag name from request scope --->
	<cfif isDefined("global.ui.form.#field#.name.#att.name#.html")>
		<cfset structAppend(config, global.ui.form[field].name[att.name].html, false)/>	
	</cfif>
	<!--- Adding html attributes of file tag from request scope--->
	<cfif isDefined("global.ui.form.#field#.html")>
		<cfset structAppend(att, global.ui.form[field].html,false)/>
	</cfif>		
	<!--- Adding html attributes of all form tags from request scope--->
	<cfif isDefined("global.ui.form.all.html")>
		<cfset structAppend(att, global.ui.form.all.html,false)/>
	</cfif>
	<cfreturn att/>
</cffunction>

<!--- 5. Function to get html and config field variables 	
<cffunction name="getFieldValues">
	<cfargument name="field">
	<cfargument name="att" type="struct"/>
	<cfargument name="config" type="struct"/>
	<cfargument name="parent"/>
	<cfargument name="global"/>
	<cfif isDefined("global.ui.form.#field#.name.#att.name#.config")>
		<cfset structAppend(config, global.ui.form[field].name[att.name].config, false)/>
	</cfif>
	<cfif isDefined("global.ui.form.#field#.name.#att.name#.html")>
		<cfset structAppend(att, global.ui.form[field].name[att.name].html, false)/>
	</cfif>
	<!--- Adding all remaining cf_form config variables --->
	<cfset structAppend(config, parent.config, false)/>
</cffunction>		
--->

<!--- 6. Function to show exception --->
<cffunction name="showException" output="true" returntype="string">
	<cfargument name="tag"/>
	<cfargument name="message"/>
	<cfargument name="level" default="3"/>
	<cfargument name="combination"/>
	<!--- Level 1 is directly on the calling page. Level 2 is in custom tag.  level 3 is in a method in custom tag.  Level 4 is in a method in a method in custom tag --->
	<cftry>
		<cfthrow message="#message#"/>
		<cfcatch type="any">
			<cfoutput>
			<h2 style="color:red;font-weight:bold">CF_#ucase(tag)# Exception:</h2> 
			Don't freak out! This is just a CFTagLib exception.  It can easily be corrected.
			<br/><br/><strong>Location:</strong>
			<cfloop from="#level#" to="#arrayLen(cfcatch.tagContext)#" index="i">
				<cfset template = cfcatch.TagContext[i].template/>
				<cfif len(template) gt len(tag) and listLast(template,'\') neq '#tag#.cfm'>
					#cfcatch.TagContext[i].template# - line #cfcatch.TagContext[i].line#<br/>
					<cfbreak/>	
				</cfif>				
			</cfloop>
			<strong>Exception Message:</strong> <span style="color:red">#cfcatch.Message#</span><br/>
			<cfif structKeyExists(arguments,'combination')>
			#combination#
			</cfif>
			<br/>
			You may set any or all attributes for all form fields through request.ui.form.<strong>tag</strong>.config.<strong>attribute</strong> or request.ui.form.<strong>tag</strong>.html.<strong>attribute</strong>.
			<h3>Please correct the exception to procceed!</h3>
			</cfoutput><cfabort/>
		</cfcatch>
	</cftry>
</cffunction>

<!--- 7. Function to evaluate a variable if exists --->
<cffunction name="exists">
	<cfargument name="name"/>
	<cfargument name="caller" type="struct"/>
	<cfargument name="eval" type="boolean" default="true"/>
	<cfif findOneOf("()",name) and parent.isObjectCall(name,caller)>
		<cfreturn parent.processObjectCall(name,caller,eval)/>
	<cfelseif structKeyExists(caller,name)>
		<cfif isCustomFunction(caller[name])>
			<cfif eval>
				<cfreturn evaluate('caller.#name#()')/>					
			<cfelse>
				<cfreturn "caller.#name#()"/>				
			</cfif>
		<cfelse>
			<cfif eval>
				<cfreturn caller[name]/>
			<cfelse>
				<cfreturn "caller.#name#"/>
			</cfif>				
		</cfif>		
	</cfif>
	<cfreturn name/>
</cffunction>

<!--- 8. Function to process value --->
<cffunction name="processValue">
	<cfargument name="value"/>
	<cfargument name="caller"/>
	<cfargument name="attributes"/>
	<cfargument name="config" default="#attributes#"/>
	<cfargument name="eval" default="true"/>
	<!--- If object call --->
	<cfif findOneOf("()",value)>
		<cfif parent.isObjectCall(value,caller)>
			<cfreturn parent.processObjectCall(value,caller,eval)/>
		<cfelseif structKeyExists(config, 'default')>
			<cfreturn htmlEditFormat(trim(config.default))/>
		</cfif>
		<!--- If the value of attributes.name available in fusebox attributes scope --->						
	<cfelseif structKeyExists(caller.attributes,attributes.name)>
		<cfreturn htmlEditFormat(trim(caller.attributes[attributes.name]))/>
		
	<cfelseif (structKeyExists(caller,value) OR isDefined(value))>
		<!--- If value available in the variables(caller) scope --->
		<cfif structKeyExists(caller,value)>
			<cfif isCustomFunction(caller[value])>
				<cfreturn htmlEditFormat(trim(evaluate("caller.#value#()")))/>
			<cfelse>
				<cfreturn htmlEditFormat(trim(caller[value]))/>
			</cfif>
		<!--- If value available in the request, session or application scope --->
		<cfelseif isDefined(value) and isCustomFunction(value)>
			<cfreturn htmlEditFormat(trim(evaluate("#value#()")))/>
		<cfelse>
			<cfreturn htmlEditFormat(trim(evaluate(value)))/>
		</cfif>
		
		<!--- If default variable is specified --->
	<cfelseif structKeyExists(config, 'default')>
		<cfreturn htmlEditFormat(trim(config.default))/>
	</cfif>
	<!--- All else --->
	<cfreturn ""/>
</cffunction>

<!--- 9. Set value --->
<cffunction name="setValue">
	<cfargument name="first"/>
	<cfargument name="second"/>
	<cfargument name="struct"/>
	<cfargument name="att" default="#struct#"/>
	<cfset var value = struct[first]/>
	<!--- If it is an objbect call --->
	<cfif listlen(struct[first],'(') neq 1>
		<cfset struct[first] = trim(listLast(listFirst(struct[first],'('),'.'))/>
		<!--- strip out the prefix get, find, or list --->
		<cfif left(struct[first],3) eq "get">
			<cfset removeChars(struct[first],len(struct[first])-3,3)/>
		<cfelseif  listfindNocase("find,list",left(struct[first],4))>
			<cfset removeChars(struct[first],len(struct[first])-4,4)/>
		</cfif>
	<!--- If a struct, take the name of the struct key --->
	<cfelseif listlen(struct[first],' ,') eq 1>
		<cfset struct[first] = listLast(struct[first],'.')/>			
	</cfif>
	<!--- if doesn't exist, take the first value --->
	<cfif len(second) and not structKeyExists(att, second)>
		<cfset att[second] = struct[first]/>
	</cfif>
	<!--- return the original value --->
	<cfreturn value/>
</cffunction>

<!--- 10. Process Object --->
<cffunction name="processObjectCall">
	<cfargument name="name"/>
	<cfargument name="caller" type="struct"/>
	<cfargument name="eval" type="boolean" default="true"/>
	<cfset var item = listFirst(name,'(')/>
	<cfset structAppend(variables,caller,false)/>
	<!--- If the object is in variable scope --->
	<cfif isCustomFunction(caller[item])>
		<cfif eval>
			<cfreturn evaluate(name)/>
		<cfelse>
			<cfreturn name/>
		</cfif>
	<!--- If the object is in request, session, or application scope --->
	<cfelseif isCustomFunction(item)>
		<cfif eval>
			<cfreturn evaluate(name)/>
		<cfelse>
			<cfreturn name/>
		</cfif>
	</cfif>
	<cfreturn ""/>
</cffunction>

<!--- 11. Is Object Call --->
<cffunction name="isObjectCall">
	<cfargument name="name"/>
	<cfargument name="caller" type="struct"/>
	<cfset var item = listFirst(name,'(')/>
	<cfif structKeyExists(caller, item) and isCustomFunction(caller[item])>
		<cfreturn true/>
	<cfelseif isDefined(item) and isCustomFunction(item)>
		<cfreturn true/>
	</cfif>
	<cfreturn false/>
</cffunction>